# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Simon Legner <Simon.Legner@gmail.com>
# Contributor: Chris Molozian (novabyte) <chris DOT molozian AT gmail DOT com>
# Contributor: Sanjuro Makabe (itti) <vuck AT gmx DOT de>

pkgbase=gradle
pkgname=('gradle' 'gradle-doc' 'gradle-src')
pkgver=8.4
_commit_hash='e9251e572c9bd1d01e503a0dfdf43aedaeecdc3f' # git rev-parse v${_tag_name}
pkgrel=1
pkgdesc='Powerful build system for the JVM'
url='https://gradle.org/'
arch=('any')
license=('Apache')
depends=('java-environment' 'bash' 'which' 'coreutils' 'findutils' 'sed')
makedepends=('git' 'asciidoc' 'xmlto' 'groovy' 'java-environment=11')
source=(https://services.gradle.org/distributions/${pkgbase}-${pkgver}-src.zip
        https://services.gradle.org/distributions/${pkgbase}-${pkgver}-all.zip
        ${pkgbase}.sh
        externalize-release-commit-hash.patch)
sha256sums=('8c6dbc4a3d9118bf16aa120106d4dfb06950a195b15715cf4843415637d2ab5a'
            'f2b9ed0faf8472cbe469255ae6c86eddb77076c75191741b4a462f33128dd419'
            '6f3472486278252417af49196847ba465b56819d286658fcdf918687f89ee032'
            'cd68dcb8bb909202c1a10181980c35bc6f11b8557618e1369464a32b718c17a0')
sha512sums=('6bee721038f6e581e5d173425e10d94939954d03e1d8eaf3e4888a76aa2216e8f46d36edb49f50d61002d271e45ce6dbcb0fc46c2c2372429d09d589e02c5aa9'
            '52d5f622b8ad0ab36ff84589733b47423b12f94211086a84eca9810690af29e396877f1155ee31279c4325e98cd1d55b32137d52efcf48eb1e258caeece08f22'
            'a50b6cf8281b56b80f55a20ac9316e1eed6887da1d191ad575dec140c9819711644d7077c4dc693b8cb0f1b08ceba0033ba88b5ad138d33ffb73b786c0d4bf81'
            '93df9d173da82b15fce0cfbcbeca0f96cd0648cb77bbaef1da383bd6754d0ffd1409b4b5628560a75cd617f4b407b4c07df3af916835914d852e1fbd36fe01e3')

prepare() {
  cd ${pkgbase}-${pkgver}
  # remove ADOPTIUM contraint from all build related files
  sed -i '/JvmVendorSpec.ADOPTIUM/d' \
    build-logic/jvm/src/main/kotlin/gradlebuild.unittest-and-compile.gradle.kts \
    subprojects/docs/src/snippets/java/toolchain-filters/groovy/build.gradle \
    subprojects/docs/src/snippets/java/toolchain-filters/kotlin/build.gradle.kts \
    build-logic-commons/gradle-plugin/build.gradle.kts \
    build-logic-commons/commons/build.gradle.kts \
    build-logic-commons/commons/src/main/kotlin/common.kt \
    build-logic-commons/code-quality-rules/build.gradle.kts

  # inhibit automatic download of binary gradle
  sed -i "s#distributionUrl=.*#distributionUrl=file\:${srcdir}/${pkgbase}-${pkgver}-all.zip#" \
    gradle/wrapper/gradle-wrapper.properties
  # https://github.com/gradle/gradle/issues/26805
  patch -Np1 < "${srcdir}/externalize-release-commit-hash.patch"
}

build() {
  cd ${pkgbase}-${pkgver}
  # requires java language level 6, which >=13 has dropped
  export PATH="/usr/lib/jvm/java-11-openjdk/bin:${PATH}"
  export COMMIT_HASH="${_commit_hash}"
  ./gradlew installAll \
    -Porg.gradle.java.installations.auto-download=false \
    -PfinalRelease=true \
    -Pgradle_installPath="$(pwd)/dist" \
    --no-configuration-cache --stacktrace
}

package_gradle() {
  cd ${pkgbase}-${pkgver}/dist
  optdepends=(
    'gradle-doc: gradle documentation'
    'gradle-src: gradle sources'
  )

  # install profile.d script
  install -Dm 755 "${srcdir}"/${pkgbase}.sh "${pkgdir}"/etc/profile.d/${pkgbase}.sh

  # create the necessary directory structure
  install -d "${pkgdir}"/usr/share/java/${pkgname}/bin
  install -d "${pkgdir}"/usr/share/java/${pkgname}/lib/{plugins,agents}
  install -d "${pkgdir}"/usr/share/java/${pkgname}/init.d
  install -d "${pkgdir}"/usr/bin

  # copy across jar files
  install -Dm 644 lib/*.jar "${pkgdir}"/usr/share/java/${pkgname}/lib
  install -Dm 644 lib/plugins/*.jar "${pkgdir}"/usr/share/java/${pkgname}/lib/plugins
  install -Dm 644 lib/agents/*.jar "${pkgdir}"/usr/share/java/${pkgname}/lib/agents

  # copy across supporting text documentation and scripts
  install -m 644 NOTICE "${pkgdir}"/usr/share/java/${pkgname}
  install -m 755 bin/gradle "${pkgdir}"/usr/share/java/${pkgname}/bin
  install -m 644 init.d/*.* "${pkgdir}"/usr/share/java/${pkgname}/init.d

  # link gradle script to /usr/bin
  ln -s /usr/share/java/${pkgname}/bin/${pkgname} "${pkgdir}"/usr/bin
}

package_gradle-doc() {
  pkgdesc+=' (documentation)'
  options=('!strip')

  cd ${pkgbase}-${pkgver}/dist
  install -d "${pkgdir}"/usr/share/java/gradle/docs
  cp -r docs/* "${pkgdir}"/usr/share/java/gradle/docs
}

package_gradle-src() {
  pkgdesc+=' (sources)'
  options=('!strip')

  cd ${pkgbase}-${pkgver}/dist
  install -d "${pkgdir}"/usr/share/java/gradle/src
  cp -r src/* "${pkgdir}"/usr/share/java/gradle/src
}

# vim: ts=2 sw=2 et:
